﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FACUI.BasicClass
{
    public class GetMapItem
    {
        private StationCfg staCfg;

        public GetMapItem(StationCfg stacfg)
        {
            this.staCfg = stacfg;
        }

        public Dictionary<string, MapLogicKglItem> GetLogicKglItems()
        {
            Dictionary<string, MapLogicKglItem> dictionary = new Dictionary<string, MapLogicKglItem>();
            CfgNode subNode = this.staCfg.GetMapInfoCfgNode().GetSubNode("逻辑开关量", true);
            foreach (KeyValuePair<string, string> pair in subNode.GetValDict())
            {
                string[] strArray = this.SplitTrimString(pair.Value);
                if (strArray.Length >= 3)
                {
                    MapLogicKglItem item = new MapLogicKglItem();
                    string str = strArray[0];
                    if (!string.IsNullOrEmpty(str))
                    {
                        string[] strArray2;
                        item.LogicKglAd = (0x7fff + Convert.ToInt32(pair.Key)).ToString();
                        item.KglName = str;
                        item.Relation = Enum.GetName(typeof(logicRelation), Convert.ToInt32(strArray[1]));
                        item.DelFlag = 1;
                        int num = Convert.ToInt32(strArray[2]);
                        if ((num == 2) && (strArray.Length >= 5))
                        {
                            strArray2 = strArray[3].Split(new char[] { ';', (char)0xff1b });
                            item.SubKgl1 = strArray2[0];
                            item.Not1 = strArray2[1] == "1";
                            strArray2 = strArray[4].Split(new char[] { ';', (char)0xff1b });
                            item.SubKgl2 = strArray2[0];
                            item.Not2 = strArray2[1] == "1";
                        }
                        else if ((num == 3) && (strArray.Length >= 6))
                        {
                            strArray2 = strArray[3].Split(new char[] { ';', (char)0xff1b });
                            item.SubKgl1 = strArray2[0];
                            item.Not1 = strArray2[1] == "1";
                            strArray2 = strArray[4].Split(new char[] { ';', (char)0xff1b });
                            item.SubKgl2 = strArray2[0];
                            item.Not2 = strArray2[1] == "1";
                            strArray2 = strArray[5].Split(new char[] { ';', (char)0xff1b });
                            item.SubKgl3 = strArray2[0];
                            item.Not3 = strArray2[1] == "1";
                        }
                        if (!dictionary.ContainsKey(str))
                        {
                            dictionary.Add(str, item);
                        }
                    }
                }
            }
            return dictionary;
        }

        public Dictionary<string, MapDcItem> GetMapDcItems(int subType)
        {
            Dictionary<string, MapDcItem> dictionary = new Dictionary<string, MapDcItem>();
            List<MapDevBase> mapItems = this.GetMapItems("sbDC", subType);
            CfgNode subNode = this.staCfg.GetMapInfoCfgNode().GetSubNode("道岔", true);
            string str3 = "";
            if (subNode != null)
            {
                foreach (MapDevBase base2 in mapItems)
                {
                    string key = base2.DevNum.ToString();
                    string instr = subNode.GetValue(key);
                    string[] strArray = this.SplitTrimString(instr);
                    if (strArray != null)
                    {
                        MapDcItem item = new MapDcItem
                        {
                            MapDevAd = strArray[0],
                            MapDevName = base2.MapDevName
                        };
                        for (int i = 2; i < strArray.Length; i++)
                        {
                            string[] strArray2 = strArray[i].Split(new char[] { ';', (char)0xff1b });
                            if ((strArray2 != null) && (strArray2.Length >= 3))
                            {
                                str3 = (strArray2[1] == "1") ? ((0x8000 + Util.ToInt(strArray2[2], 0))).ToString() : strArray2[2];
                                switch (i)
                                {
                                    case 2:
                                        item.Dkgl = strArray2[0];
                                        item.DkglAD = str3;
                                        break;

                                    case 3:
                                        item.Fkgl = strArray2[0];
                                        item.FkglAD = str3;
                                        break;

                                    case 4:
                                        item.qRed = strArray2[0];
                                        item.qRedAD = str3;
                                        break;

                                    case 5:
                                        item.qWhite = strArray2[0];
                                        item.qWhiteAD = str3;
                                        break;

                                    case 6:
                                        item.dRed = strArray2[0];
                                        item.dRedAD = str3;
                                        break;

                                    case 7:
                                        item.dWhite = strArray2[0];
                                        item.dWhiteAD = str3;
                                        break;

                                    case 8:
                                        item.fRed = strArray2[0];
                                        item.fRedAD = str3;
                                        break;

                                    case 9:
                                        item.fWhite = strArray2[0];
                                        item.fWhiteAD = str3;
                                        break;
                                }
                            }
                        }
                        if (!dictionary.ContainsKey(base2.MapDevName))
                        {
                            dictionary.Add(base2.MapDevName, item);
                        }
                    }
                }
            }
            return dictionary;
        }

        private List<MapDevBase> GetMapItems(string devtype, int subtype)
        {
            List<MapDevBase> list = new List<MapDevBase>();
            int num = 0;
            CfgNode subNode = this.staCfg.GetMapInfoCfgNode().GetSubNode("详述", true);
            foreach (KeyValuePair<string, string> pair in subNode.GetValDict())
            {
                string[] strArray = this.SplitTrimString(pair.Value);
                if (strArray.Length >= 3)
                {
                    MapDevBase base2;
                    string str = strArray[0];
                    if (string.IsNullOrEmpty(str))
                    {
                        str = "DUMMY";
                    }
                    string str2 = strArray[1];
                    string str3 = strArray[2];
                    if ((str2 == devtype) && (subtype != -1))
                    {
                        num++;
                        if (str3 == subtype.ToString())
                        {
                            base2 = new MapDevBase
                            {
                                MapDevName = str,
                                MapDevType = str2,
                                MapDevSupType = str3,
                                DevNum = num
                            };
                            list.Add(base2);
                        }
                    }
                    else if ((str2 == devtype) && (subtype == -1))
                    {
                        num++;
                        base2 = new MapDevBase
                        {
                            MapDevName = str,
                            MapDevType = str2,
                            MapDevSupType = str3,
                            DevNum = num
                        };
                        list.Add(base2);
                    }
                }
            }
            return list;
        }

        public Dictionary<string, MapLAnItem> GetMapLANItems(int subType)
        {
            Dictionary<string, MapLAnItem> dictionary = new Dictionary<string, MapLAnItem>();
            List<MapDevBase> mapItems = this.GetMapItems("sbDHAN", subType);
            CfgNode subNode = this.staCfg.GetMapInfoCfgNode().GetSubNode("灯或按钮", true);
            string str3 = "";
            if (subNode != null)
            {
                foreach (MapDevBase base2 in mapItems)
                {
                    string key = base2.DevNum.ToString();
                    string instr = subNode.GetValue(key);
                    string[] strArray = this.SplitTrimString(instr);
                    if (strArray != null)
                    {
                        MapLAnItem item = new MapLAnItem
                        {
                            MapDevAd = strArray[0],
                            MapDevName = base2.MapDevName
                        };
                        for (int i = 1; i < strArray.Length; i++)
                        {
                            string[] strArray2 = strArray[i].Split(new char[] { ';', (char)0xff1b });
                            if ((strArray2 != null) && (strArray2.Length >= 3))
                            {
                                str3 = (strArray2[1] == "1") ? ((0x8000 + Util.ToInt(strArray2[2], 0))).ToString() : strArray2[2];
                                switch (i)
                                {
                                    case 1:
                                        item.SignalFlag = strArray2[0];
                                        item.SignalFlagAD = str3;
                                        break;

                                    case 2:
                                        item.Green = strArray2[0];
                                        item.GreenAD = str3;
                                        break;

                                    case 3:
                                        item.Yellow = strArray2[0];
                                        item.YellowAD = str3;
                                        break;

                                    case 4:
                                        item.Red = strArray2[0];
                                        item.RedAD = str3;
                                        break;

                                    case 5:
                                        item.Blue = strArray2[0];
                                        item.BlueAD = str3;
                                        break;

                                    case 6:
                                        item.White = strArray2[0];
                                        item.WhiteAD = str3;
                                        break;
                                }
                            }
                        }
                        if (!dictionary.ContainsKey(base2.MapDevName))
                        {
                            dictionary.Add(base2.MapDevName, item);
                        }
                    }
                }
            }
            return dictionary;
        }

        public Dictionary<string, MapWcqdItem> GetMapWcqdItems(int subType)
        {
            Dictionary<string, MapWcqdItem> dictionary = new Dictionary<string, MapWcqdItem>();
            List<MapDevBase> mapItems = this.GetMapItems("sbWCQD", subType);
            CfgNode subNode = this.staCfg.GetMapInfoCfgNode().GetSubNode("无岔区段", true);
            string str3 = "";
            if (subNode != null)
            {
                foreach (MapDevBase base2 in mapItems)
                {
                    string key = base2.DevNum.ToString();
                    string instr = subNode.GetValue(key);
                    string[] strArray = this.SplitTrimString(instr);
                    if (strArray != null)
                    {
                        MapWcqdItem item = new MapWcqdItem
                        {
                            MapDevAd = strArray[0],
                            MapDevName = base2.MapDevName
                        };
                        if (base2.MapDevName == "IIIAG")
                        {
                        }
                        for (int i = 1; i < strArray.Length; i++)
                        {
                            string[] strArray2 = strArray[i].Split(new char[] { ';', (char)0xff1b });
                            if ((strArray2 != null) && (strArray2.Length >= 3))
                            {
                                str3 = (strArray2[1] == "1") ? ((0x8000 + Util.ToInt(strArray2[2], 0))).ToString() : strArray2[2];
                                switch (i)
                                {
                                    case 1:
                                        item.gdRed = strArray2[0];
                                        item.gdRedAD = str3;
                                        break;

                                    case 2:
                                        item.gdWhite = strArray2[0];
                                        item.gdWhiteAD = str3;
                                        break;
                                }
                            }
                        }
                        if (!dictionary.ContainsKey(base2.MapDevName))
                        {
                            dictionary.Add(base2.MapDevName, item);
                        }
                    }
                }
            }
            return dictionary;
        }

        public Dictionary<string, MapXhjItem> GetMapXHJItems(int subType)
        {
            Dictionary<string, MapXhjItem> dictionary = new Dictionary<string, MapXhjItem>();
            List<MapDevBase> mapItems = this.GetMapItems("sbXHJ", subType);
            CfgNode subNode = this.staCfg.GetMapInfoCfgNode().GetSubNode("信号机", true);
            string str3 = "";
            if (subNode != null)
            {
                foreach (MapDevBase base2 in mapItems)
                {
                    string key = base2.DevNum.ToString();
                    string instr = subNode.GetValue(key);
                    if (instr.Contains("D"))
                    {
                    }
                    string[] strArray = this.SplitTrimString(instr);
                    if (strArray != null)
                    {
                        MapXhjItem item = new MapXhjItem
                        {
                            MapDevAd = strArray[0],
                            MapDevName = strArray[1]
                        };
                        for (int i = 2; i < strArray.Length; i++)
                        {
                            string[] strArray2 = strArray[i].Split(new char[] { ';', (char)0xff1b });
                            if ((strArray2 != null) && (strArray2.Length >= 3))
                            {
                                str3 = (strArray2[1] == "1") ? ((0x8000 + Util.ToInt(strArray2[2], 0))).ToString() : strArray2[2];
                                switch (i)
                                {
                                    case 2:
                                        item.Red = strArray2[0];
                                        item.RedAD = str3;
                                        break;

                                    case 3:
                                        item.Green = strArray2[0];
                                        item.GreenAD = str3;
                                        break;

                                    case 4:
                                        item.Yellow = strArray2[0];
                                        item.YellowAD = str3;
                                        break;

                                    case 5:
                                        item.White = strArray2[0];
                                        item.WhiteAD = str3;
                                        break;

                                    case 6:
                                        item.Blue = strArray2[0];
                                        item.BlueAD = str3;
                                        break;

                                    case 7:
                                        item.DGreen = strArray2[0];
                                        item.DGreenAD = str3;
                                        break;

                                    case 8:
                                        item.DYellow = strArray2[0];
                                        item.DYellowAD = str3;
                                        break;

                                    case 9:
                                        item.YellowFlashYellow = strArray2[0];
                                        item.YellowFlashYellowAD = str3;
                                        break;

                                    case 10:
                                        item.RedFlash = strArray2[0];
                                        item.RedFlashAD = str3;
                                        break;

                                    case 11:
                                        item.WhiteFlash = strArray2[0];
                                        item.WhiteFlashAD = str3;
                                        break;

                                    case 12:
                                        item.GreenFlash = strArray2[0];
                                        item.GreenFlashAD = str3;
                                        break;

                                    case 13:
                                        item.YellowFlash = strArray2[0];
                                        item.YellowFlashAD = str3;
                                        break;

                                    case 14:
                                        item.GreenYellow = strArray2[0];
                                        item.GreenYellowAD = str3;
                                        break;

                                    case 15:
                                        item.RedWhite = strArray2[0];
                                        item.RedWhiteAD = str3;
                                        break;
                                }
                            }
                        }
                        if (!dictionary.ContainsKey(base2.MapDevName))
                        {
                            dictionary.Add(base2.MapDevName, item);
                        }
                    }
                }
            }
            return dictionary;
        }

        private string[] SplitTrimString(string instr)
        {
            if (string.IsNullOrEmpty(instr))
            {
                return null;
            }
            return instr.Split(new char[] { ',', (char)0xff0c });
        }

        public enum logicRelation
        {
            空操作,
            与操作,
            或操作,
            异或操作,
            与反操作,
            或反操作
        }
    }
}

